package com.facedemo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.facedemo.utils.LogUtils;

public class CameraSurface extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder gHolder;
    private SurfaceViewCallback surfaceViewCallback;

    public CameraSurface(Context context) {
        this(context, null);
        getHolder().addCallback(this);
    }

    public CameraSurface(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);
    }

    public void setSurfaceCallback(SurfaceViewCallback surfaceViewCallback) {
        this.surfaceViewCallback = surfaceViewCallback;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        LogUtils.printI("CameraSurface-->surfaceChanged h:"+height+" w:"+width);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        gHolder = holder;
        if(surfaceViewCallback!=null) {
            surfaceViewCallback.surfaceCreated();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        gHolder = null;
        if(surfaceViewCallback!=null) {
            surfaceViewCallback.surfaceDestroyed();
        }
    }

    public SurfaceHolder getCameraSufaceHolder() {
        return gHolder;
    }

    public interface SurfaceViewCallback {
        public void surfaceCreated();
        public void surfaceDestroyed();
    }
}
